var app = getApp();
var ux = require('../../../lib/ux.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    couponCount:0,
    yqtype:false,
    xinren:false,
    lbs:[],
    authDZs:false,
    coupon:{},
    province: '',
    city: '',
    latitude: '',
    longitude: '',
    agentType:0,
    id:null,
    userId:null,  
    images: [],
    merchant:{},
    userInfo:{},
    wordList:[],
    getUrlYQ:"/api/memberCoupon/getNewCouponsYQ.do",
    getUrl:"/api/memberCoupon/getNewCoupons.do",
    yjyqUrl:"/api/memberCoupon/getYjyqCouponData.do",
    action: "/api/tabOne/gotoBrandHome.do",
    agentUrls: "/api/memberAgent/isApplyAgent.do",
    guanZhuUrl:"/api/memberOhter/getGzBs.do",
    submit: "{{submit}}"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var self =this;
    var setting = wx.getStorageSync("setting");
    var pamasss ={};
    if (options.id){
      this.setData({
        id: parseInt(options.id)
      })
      wx.setStorageSync("banrdId", options.id);
      pamasss.banrdId = Number(options.id);
    } else if (options.userId){
      this.setData({
        id: parseInt(options.banrdId),
        userId: parseInt(options.userId)
      })
      wx.setStorageSync("banrdId", options.banrdId);
      pamasss.otherId = parseInt(options.userId);
      pamasss.banrdId = parseInt(options.banrdId);
      // var pamasss = { otherId: options.userId, banrdId: options.banrdId}
      // ux.loginBand(pamasss, function (data) {
      // })
    } else if (wx.getStorageSync("banrdId")){
      this.setData({
        id: parseInt(wx.getStorageSync("banrdId"))
      })
      pamasss.banrdId = Number(parseInt(wx.getStorageSync("banrdId")));
     }else{
      wx.reLaunch({
        url: '/pages/brandSelect',
      })
     }
    ux.loginCallback(pamasss, function (data) {
      if (data.data.getnew){
           self.setData({
             xinren:true,
             coupon: data.data.coupon
           })
        wx.setNavigationBarTitle({
          title: data.data.coupon.merchant$,
        })
      }
      // if(data.data.returnCoupon){
      //   self.setData({
      //     yqtype:true,
      //     coupon: data.data.rcoupon
      //   })
      // }
        self.getInitData();
      // if(data.data.type==2){
      //   self.getInitData();
      // }else{
      //   self.getInitData();
      // }
     })
  },
  
  goToLingYQ:function(){
   
    var self = this;
    var pass = {
      id: this.data.coupon.id
    }
    wx.showLoading({
      title: '领取中...',
    })
    ux.request(this.data.getUrlYQ, pass, function (protocol) {
      wx.hideLoading();
      self.setData({
         yqtype: false
      })
      self.toast(protocol.data.msg);
    });
  },
  goToLing:function(){
   
    var self = this;
    var pass = {
      id: this.data.coupon.id
    }
    wx.showLoading({
      title: '领取中...',
    })
    ux.request(this.data.getUrl, pass, function (protocol) {
      wx.hideLoading();
      self.setData({
        xinren: false
      })
      self.toast(protocol.data.msg);
    });
  },
  getInitData: function (){
    
    var self=this;
    if (this.data.action) {
      wx.showLoading({
        title: '加载中.....',
      })
      var bparmas = { banrdId: parseInt(self.data.id)}
      ux.request("/api/memberUser/getBanrdPageData.do", bparmas, function (protocol1){
        wx.setStorageSync("banrd", protocol1.data.banrd);
        wx.setStorageSync("banrdId", self.data.id);
        wx.setStorageSync("parameter", protocol1.data.parameter);
        console.log(JSON.stringify(protocol1.data.lbs));
        self.setData({
          couponCount:protocol1.data.couponCount,
          lbs: protocol1.data.lbs,
          wordList: protocol1.data.list,
          pam: protocol1.data.parameter,
          images: protocol1.data.parameter.homeRotary ? protocol1.data.parameter.homeRotary.split(",") : [],
          merchant: protocol1.data.banrd
        })
        wx.setNavigationBarTitle({
          title: protocol1.data.banrd.brandName,
        })
        wx.hideLoading();
      }, true);
    }
  },
  lbToPage:function(e){
  
  var dt=e.currentTarget.dataset;
  var obj=this.data.lbs[dt.idx];
  console.log(JSON.stringify(obj));
  if(obj.tbccType==1){
    if(obj.tbccCommodity){
      var parmas={id:obj.tbccCommodity}
      wx.showLoading({
        title: '加载中...',
        mask:true
      })
      ux.request("/api/memberShop/goodsDetail.do", parmas, function (protocol) {
        console.log(JSON.stringify(protocol.data))
        wx.hideLoading();
        wx.navigateTo({
          url: '/pages/member/tab1/shop/detail?id='+obj.tbccCommodity+"&prices="+protocol.data.entity.price+"&sum="+protocol.data.entity.price,
        })
      },true)
    }else{
    }
  }else if(obj.tbccType==2){
      wx.navigateTo({
        url: '/pages/member/tab3/other/wzpage?type=hd&id='+obj.tbccActivity,
      })
  }else if(obj.tbccType==3){
    wx.navigateTo({
      url: '/pages/member/tab1/agent/cardPay?banrdId=' + wx.getStorageSync("banrdId"),
    })
  }else if(obj.tbccType==4){
    wx.navigateTo({
      url: '/pages/member/tab1/agent/hhrCard?long=' + wx.getStorageSync("longitude") + '&lan=' + wx.getStorageSync("latitude") + '&banrdId=' + wx.getStorageSync("banrdId"),
    })
  }else if(obj.tbccType==5){
   wx.navigateTo({
    url: '/pages/member/tab3/recharge/recharge?province=' + wx.getStorageSync("province") + "&city=" + wx.getStorageSync("city")+'&tab=1',
   })
  }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var that = this;
    wx.showShareMenu({
      withShareTicket: true
    })
   
    var userInfo = wx.getStorageSync("userInfo");
    var obj = this.data.entity;
    var shareObj = {
      title: wx.getStorageSync("setting").coverTitle,
      imageUrl: wx.getStorageSync("setting").coverImg,
      path: '/pages/member/tab1/sugarSpice?userId=' + userInfo.uid + "&banrdId=" + that.data.id,
      success: function (res) {
        // 转发成功之后的回调
        if (res.errMsg == 'shareAppMessage:ok') {
          that.toast("转发成功");
        }
      },
      fail: function () {
        // 转发失败之后的回调
        if (res.errMsg == 'shareAppMessage:fail cancel') {
          that.toast("取消转发");
        } else if (res.errMsg == 'shareAppMessage:fail') {
          that.toast("转发失败");
        }
      }
    }
    return shareObj;
  },
 toAgent:function(){
   var pa = { long: wx.getStorageSync("longitude"), lan: wx.getStorageSync("latitude")}
   wx.navigateTo({
     url: '/pages/member/tab1/agent/applyAgent?pa=' + JSON.stringify(pa),
   })
 },
  onForm: function (event) {
    if (this.data.submit) {
      var $this = this;
      var dataset = event.currentTarget.dataset;
      ux.request(this.data.submit, detail.value, function (protocol) {
        if (protocol.success) {
          if (dataset.url) {
            wx.redirectTo(dataset.url);
          }
        }
      }, true);
    }
  },

  onTap: function (event) {
    var dataset = event.currentTarget.dataset;
    var data = this.data;
    if (dataset.key && dataset.val) {
      data[dataset.key] = dataset.val;
    }
    this.setData(data);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var userInfo = wx.getStorageSync("userInfo");
    if (userInfo.authStatus==0){
      this.setData({
        authDZs: true
      })
    }else{
      this.setData({
        authDZs: false
      })
    }
  },
  authDZ:function(){
    wx.showModal({
      title: '操作提醒',
      content: '当前，您未微信授权，请前往我的页面进行授权登录',
      success: function (res) {
        if (res.cancel) {
        } else if (res.confirm) {
          wx.switchTab({
            url: "/pages/member/tab3/MyCenter",
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
 
  gotoSelect:function(){
    wx.reLaunch({
      url: '/pages/brandSelect',
    })
  },
  toYjyq:function(){
    var pamars = { banrdId: this.data.id };
    var self = this;
    ux.request(this.data.yjyqUrl, pamars, function (protocol) {
      if (protocol.data.obj.id) {
        wx.navigateTo({
          url: '/pages/member/tab3/coupon/prizeInvitation',
        })
      } else {
        self.toast("当前品牌还没有投放有奖邀请！");
      }
    }, true);
  },
  toGuanZhu:function(){
    var pamars = { banrdId:this.data.id};
    var self=this;
    ux.request(this.data.guanZhuUrl, pamars, function (protocol) {
      if (protocol.data.gzyl && protocol.data.gzyl.putIn==1){
            wx.navigateTo({
              url: '/pages/member/tab1/articel/followGift',
            })
         }else{
          self.toast("当前品牌还没有关注有礼！");
       }
    }, true);
  },
  toast: function (params) {
    if (typeof (params) == "string") {
      params = { content: params };
    }
    this.setData({
      toast: params.content
    });
    var THIS = this;
    setTimeout(function () {
      THIS.setData({
        toast: false
      });
      if (params.complete) {
        params.complete();
      }
    }, params.time || 3000);
  },
  toYouPin:function(){
       wx.navigateTo({
         url: '/pages/member/tab3/recharge/recharge?province=' + wx.getStorageSync("province") + "&city=" + wx.getStorageSync("city")+'&tab=0',
       })
  },
  toGoodsList:function(){
      wx.navigateTo({
        url: '/pages/member/tab1/shop/yplist?province=' + wx.getStorageSync("province") + "&city=" + wx.getStorageSync("city"),
      })
  },
 
})
